package com.ruicar.afs.cloud.afscase.cargpsmanage.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.approvetask.service.WorkProcessScheduleInfoTempService;
import com.ruicar.afs.cloud.afscase.cargpsmanage.entity.CarGpsApply;
import com.ruicar.afs.cloud.afscase.cargpsmanage.entity.CarGpsDevice;
import com.ruicar.afs.cloud.afscase.cargpsmanage.service.CarGpsApplyService;
import com.ruicar.afs.cloud.afscase.cargpsmanage.service.CarGpsDeviceService;
import com.ruicar.afs.cloud.afscase.cargpsmanage.service.GpsManageService;
import com.ruicar.afs.cloud.afscase.cargpsmanage.vo.GpsOrderInfoVO;
import com.ruicar.afs.cloud.afscase.cargpsmanage.vo.Postions;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseCarInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseCarInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.afscase.loanactivatepool.entity.LoanActivatePool;
import com.ruicar.afs.cloud.afscase.loanactivatepool.service.LoanActivateService;
import com.ruicar.afs.cloud.afscase.loanapprove.condition.LoanApproveCondition;
import com.ruicar.afs.cloud.afscase.loanapprove.service.LoanWorkflowService;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.service.ApproveLoanInfoService;
import com.ruicar.afs.cloud.afscase.processor.enums.LoanSubmitEnum;
import com.ruicar.afs.cloud.afscase.processor.enums.WorkflowType;
import com.ruicar.afs.cloud.afscase.processor.service.LoanGpsAuditWorkflowService;
import com.ruicar.afs.cloud.afscase.processor.vo.LoanWorkflowDataVO;
import com.ruicar.afs.cloud.afscase.risk.service.CaseTortoiseService;
import com.ruicar.afs.cloud.bizcommon.gps.entity.GpsOLEEimgs;
import com.ruicar.afs.cloud.bizcommon.gps.entity.GpsOLEPostions;
import com.ruicar.afs.cloud.bizcommon.gps.entity.GpsReceive;
import com.ruicar.afs.cloud.bizcommon.gps.feign.GpsOLEFeign;
import com.ruicar.afs.cloud.bizcommon.gps.service.GpsOLEService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.*;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yk.Li
 * @Date 2020/8/5
 * @description
 */
@Service
@AllArgsConstructor
@Slf4j
public class GpsManageServiceImpl implements GpsManageService {
    private final LoanWorkflowService loanWorkflowService;
    private final CaseContractInfoService caseContractInfoService;
    private final CarGpsApplyService carGpsApplyService;
    private final CaseCarInfoService carInfoService;
    private final CarGpsDeviceService carGpsDeviceService;
    private final LoanActivateService loanActivateService;
    private final LoanGpsAuditWorkflowService loanGpsAuditWorkflowService;
    private final WorkProcessScheduleInfoTempService scheduleInfoTempService;
    private final ApproveLoanInfoService approveLoanInfoService;

    private final GpsOLEService gpsOLEService;

    private final CaseTortoiseService caseTortoiseService;
    private final ComAttachmentFileService comAttachmentFileService;

    @Transactional
    @Override
    public void gpsAutoAudit() {
        log.info(">>>>>>>>GPS流程自动审批start...");
        List<CaseContractInfo> caseContractInfoList = caseContractInfoService.getAllContractInfoByWorkflowStatus();
        for (CaseContractInfo caseContractInfo : caseContractInfoList) {
            String contractNo = caseContractInfo.getContractNo();
            try {
                this.getAllOrderInfoByAppCode(contractNo);
            } catch (Exception e) {
                log.error("合同错误编号："+contractNo, e.getMessage());
            }

//            if (this.gpsPassRules(contractNo)) {
//                log.info("合同号【" + contractNo + "】GPS安装规则审核不通过");
//                continue;
//            }
//
//            //更新GPS审核状态
//            this.updateStatus(contractNo, AfsEnumUtil.key(GpsStatusEnum.PASS_AUDIT) ,null);
//            //审核完成 更新激活池GPS审核状态
//            // this.updateActivePool(contractNo);
//            log.info("更新合同：【" + contractNo + "】流程审核状态完成...【" + caseContractInfo.getGpsWorkflowStatus() + "】");

            //提交工作流
//            this.submitGpsWorkflow(caseContractInfo);
        }
        log.info(">>>>>>>>GPS流程自动审批end...");
    }

    private void updateActivePool(String contractNo) {
        LoanActivatePool pool = loanActivateService.getOne(Wrappers.<LoanActivatePool>lambdaQuery()
                .eq(StringUtils.isNotBlank(contractNo),LoanActivatePool::getContractNo,contractNo));
        if(null != pool) {
            pool.setGpsApproveStatus(WhetherEnum.YES.getCode());
            pool.setGpsApproveTime(new Date());
        }
    }

    /**
     * a)判定合格
     * a.抓取的车架号与系统车架号是否一致；
     * b.GPS状态为在线/静止/休眠/行驶中等，不可为离线状态；
     * c.安装数量满足系统派单需求；
     * d.安装设备类型是否满足派单要求，如是否为有线设备；
     * e.显示GPS供应商安装有效照片(自动判断规则，无法判断照片是否有效，只能判断有无照片。)，全部满足则GPS审批通过；
     * b)判定不合格则不通过，不能放款。派单之后立刻执行第一次抓取GPS状态，72个小时内，每隔十五分钟重新抓取一遍信息，直到判定合格为止，超过则手工刷新抓取；
     */
    @Override
    public Boolean gpsPassRules(String contractNo) {
        // 先去获取GPS信息
        if(!this.getAllOrderInfoByAppCode(contractNo)){
            log.info("合同号【{}】GPS刷新失败！", contractNo);
            return true;
        }


        CarGpsApply carGpsApply = carGpsApplyService.getOne(Wrappers.<CarGpsApply>lambdaQuery()
                .eq(StringUtils.isNotBlank(contractNo), CarGpsApply::getContractNo, contractNo));
        if (ObjectUtil.isNull(carGpsApply)) {
            log.info("合同号【{}】GPS申请信息不存在", contractNo);
            return true;
        }
        CaseCarInfo caseCarInfo = carInfoService.getOne(Wrappers.<CaseCarInfo>lambdaQuery()
                .eq(StringUtils.isNotBlank(contractNo), CaseCarInfo::getContractNo, contractNo));
        if (ObjectUtil.isNull(carGpsApply)) {
            log.info("合同号【{}】车辆申请信息不存在", contractNo);
            return true;
        }
        List<CarGpsDevice> carGpsDeviceList = carGpsDeviceService.list(Wrappers.<CarGpsDevice>lambdaQuery()
                .eq(CarGpsDevice::getOrderNo, carGpsApply.getOrderNo()));
        if (CollectionUtil.isEmpty(carGpsDeviceList)) {
            log.info("合同号【{}】车辆GPS设备信息不存在", contractNo);
            return true;
        }

        //a.抓取的车架号与系统车架号是否一致
        if (carGpsApply.getCarVin().equals(caseCarInfo.getCarVin())) {
            log.info("合同号【{}】车架号不一致，自动审核不通过", contractNo);
            return true;
        }

        Boolean gpsRunStatusFlag = Boolean.FALSE;
        String wired = new String();
        String wireless = new String();
        for (CarGpsDevice carGpsDevice : carGpsDeviceList) {
            if (!(AfsEnumUtil.key(GpsRunStatusEnum.unMove).equals(carGpsDevice.getGpsRunStatus())
                    || AfsEnumUtil.key(GpsRunStatusEnum.running).equals(carGpsDevice.getGpsRunStatus()))) {
                gpsRunStatusFlag = Boolean.TRUE;
                break;
            }
            if (AfsEnumUtil.key(GpsTypeEnum.wired).equals(carGpsDevice.getGpsType())) {
                wired = "wired";
            }
            if (AfsEnumUtil.key(GpsTypeEnum.wireless).equals(carGpsDevice.getGpsType())) {
                wireless = "wireless";
            }
        }

        //b.GPS状态为在线/静止/休眠/行驶中等，不可为离线状态
        if (gpsRunStatusFlag) {
            log.info("合同号【{}】含有GPS运行状态为【离线】/【未启用】的设备，自动审核不通过", contractNo);
            return true;
        }

        GpsInstallTypeEnum gpsType = (GpsInstallTypeEnum) AfsEnumUtil.getEnum(carGpsApply.getGpsType(), GpsInstallTypeEnum.class);
        Boolean installTypeFlag = Boolean.FALSE;
        switch (gpsType) {
            case wireless:
                if (!(StrUtil.isNotBlank(wireless) && carGpsDeviceList.size() == 1)) {
                    installTypeFlag = Boolean.TRUE;
                }
                break;
            case wired:
                if (!(StrUtil.isNotBlank(wired) && carGpsDeviceList.size() == 1)) {
                    installTypeFlag = Boolean.TRUE;
                }
                break;
            case wiredAndWireless:
                if (!(StrUtil.isNotBlank(wired) && StrUtil.isNotBlank(wireless) && carGpsDeviceList.size() == 2)) {
                    installTypeFlag = Boolean.TRUE;
                }
                break;
        }
        //c.安装数量满足系统派单需求 d.安装设备类型是否满足派单要求，如是否为有线设备；
        if(installTypeFlag){
            log.info("合同号【{}】安装设备类型不满足派单要求，自动审核不通过", contractNo);
            return true;
        }
        // 去判断有没有图片
        List<ComAttachmentFile> comAttachmentFiles=comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo,contractNo).eq(ComAttachmentFile::getBelongNo,carGpsApply.getCarId().toString()));
        if(comAttachmentFiles.size()<0){
            log.info("合同号【{}】为获取GPS安装图片", contractNo);
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public void startGpsWorkflow(String contractNo) {
        CaseContractInfo contractInfo = caseContractInfoService.getOne(Wrappers.<CaseContractInfo>lambdaQuery()
                .eq(StringUtils.isNotBlank(contractNo), CaseContractInfo::getContractNo, contractNo));
        Assert.isTrue(null != contractInfo, "合同【{" + contractInfo + "}】信息不存在");

        CarGpsApply carGpsApply = carGpsApplyService.getOne(Wrappers.<CarGpsApply>lambdaQuery().eq(CarGpsApply::getContractNo, contractNo));
        if (ObjectUtil.isNull(carGpsApply)) {
            log.info("合同【{}】工单信息不存在,流程发起中止...", contractInfo);
            return;
        } else if (StringUtils.isBlank(carGpsApply.getGpsType())) {
            log.info("合同【{}】GPS安装类型不存在,流程发起中止...", contractInfo);
            return;
        } else if (StringUtils.isNotBlank(carGpsApply.getGpsType())
                && StrUtil.equals(AfsEnumUtil.key(GpsInstallTypeEnum.noInstall), carGpsApply.getGpsType())) {
            log.info("合同【{}】GPS安装类型为【不安装】,流程发起中止...", contractInfo);
            return;
        }
        log.info("合同【{}】GPS安装类型为【{}】,流程发起中止...", contractInfo,carGpsApply.getGpsType());
        //更新Gps审核状态为审核通过
        this.updateStatus(contractNo, AfsEnumUtil.key(GpsStatusEnum.FIRST_TRIAL), null);

        // 注释掉工作流的东西

//        loanGpsAuditWorkflowService.start(LoanWorkflowDataVO.builder()
//                .workProcessScheduleInfoTemp(scheduleInfoTempService.createWorkProcessor(contractInfo, WorkflowType.LOAN_GPS_AUDIT.getAfsFlowKey()))
//                .build());
    }

    private void submitGpsWorkflow(CaseContractInfo caseContractInfo) {
        LoanApproveCondition condition = new LoanApproveCondition();
        condition.setApplyNo(caseContractInfo.getApplyNo());
        condition.setContractNo(caseContractInfo.getContractNo());
        condition.setAutoFlag(true);//自动提交标记
        this.gpsManageSubmit(condition, AfsEnumUtil.key(GpsStatusEnum.PASS_AUDIT));
    }

    @Override
    public void gpsSecondAssignmentSubmit(LoanApproveCondition condition) {
        //工作流提交流程  保存操作记录
        condition.setApproveType(ApproveTypeEnum.PROCESS.getValue());
        condition.setUseScene(UseSceneEnum.GPS_AUDIT.getValue());
        condition.setApproveSuggest(AfsEnumUtil.key(LoanSubmitEnum.SECOND_ASSIGNMENT));
        loanWorkflowService.gpsSubmitWorkflowByScheduleInfo(condition);
    }

    @Override
    public void gpsManageSubmit(LoanApproveCondition condition,String gpsStatusEnum) {
        //工作流提交流程  保存操作记录
        condition.setApproveType(ApproveTypeEnum.PROCESS.getValue());
        condition.setUseScene(UseSceneEnum.GPS_AUDIT.getValue());
        condition.setApproveSuggest(AfsEnumUtil.key(LoanSubmitEnum.SUBMIT));
        loanWorkflowService.gpsSubmitWorkflowByScheduleInfo(condition);
        //推送数据到进件系统
        approveLoanInfoService.sendToApplyNotic(condition.getContractNo(), gpsStatusEnum);
    }

    @Transactional
    @Override
    public void backPartnersSubmit(LoanApproveCondition condition) {
        //更新Gps审核状态为不合格
        this.updateStatus(condition.getContractNo(), AfsEnumUtil.key(GpsStatusEnum.UN_PASS_AUDIT) ,null);

        //工作流提交流程  保存操作记录
        // 下面的是工作流的不走了 暂时注释掉
//        condition.setApproveType(ApproveTypeEnum.PROCESS.getValue());
//        condition.setUseScene(UseSceneEnum.GPS_AUDIT.getValue());
//        condition.setApproveSuggest(AfsEnumUtil.key(LoanSubmitEnum.BACK_PARTNERS));
//        loanWorkflowService.gpsSubmitWorkflowByScheduleInfo(condition);

        //推送数据到进件系统
        approveLoanInfoService.sendToApplyNotic(condition.getContractNo(), AfsEnumUtil.key(GpsStatusEnum.UN_PASS_AUDIT));
    }

    @Override
    public void updateStatus(String contractNo, String workflowStatus ,Long carId) {
        GpsStatusEnum statusEnum = (GpsStatusEnum) AfsEnumUtil.getEnum(workflowStatus, GpsStatusEnum.class);
        switch (statusEnum) {
            case SEND_TASK:
                caseContractInfoService.updateWorkflowStatus(contractNo, GpsStatusEnum.SEND_TASK);
                carGpsApplyService.updateApplyStatus(contractNo, GpsStatusEnum.SEND_TASK,carId);
                break;
            case UN_SEND_TASK:
                caseContractInfoService.updateWorkflowStatus(contractNo, GpsStatusEnum.UN_SEND_TASK);
                carGpsApplyService.updateApplyStatus(contractNo, GpsStatusEnum.UN_SEND_TASK,carId);
                break;
            case WAIT_INSTALL:
                caseContractInfoService.updateWorkflowStatus(contractNo, GpsStatusEnum.WAIT_INSTALL);
                carGpsApplyService.updateApplyStatus(contractNo, GpsStatusEnum.WAIT_INSTALL,carId);
                break;
            case COMPLETE_INSTALL:
                caseContractInfoService.updateWorkflowStatus(contractNo, GpsStatusEnum.COMPLETE_INSTALL);
                carGpsApplyService.updateApplyStatus(contractNo, GpsStatusEnum.COMPLETE_INSTALL,carId);
                break;
            case FIRST_TRIAL:
                caseContractInfoService.updateWorkflowStatus(contractNo, GpsStatusEnum.FIRST_TRIAL);
                carGpsApplyService.updateApplyStatus(contractNo, GpsStatusEnum.FIRST_TRIAL,carId);
                break;
            case PARTNERS_AUDIT:
                caseContractInfoService.updateWorkflowStatus(contractNo, GpsStatusEnum.PARTNERS_AUDIT);
                carGpsApplyService.updateApplyStatus(contractNo, GpsStatusEnum.PARTNERS_AUDIT,carId);
                break;
            case PASS_AUDIT:
                caseContractInfoService.updateWorkflowStatus(contractNo, GpsStatusEnum.PASS_AUDIT);
                carGpsApplyService.updateApplyStatus(contractNo, GpsStatusEnum.PASS_AUDIT,carId);
                break;
            case UN_PASS_AUDIT:
                caseContractInfoService.updateWorkflowStatus(contractNo, GpsStatusEnum.UN_PASS_AUDIT);
                carGpsApplyService.updateApplyStatus(contractNo, GpsStatusEnum.UN_PASS_AUDIT,carId);
                break;
            default:
                log.info("流程审核状态不存在:{}", workflowStatus);
        }
    }

    @Override
    public Boolean getAllOrderInfoByAppCode(String contractNo) {
        Boolean flag = Boolean.TRUE;
        List<GpsOrderInfoVO> gpsOrderInfoVOList = this.updateDeviceInfo(contractNo);
        for(GpsOrderInfoVO gpsOrderInfoVO:gpsOrderInfoVOList){
            if(!gpsOrderInfoVO.getStatus()){
                flag = Boolean.FALSE;
            }
        }
        return flag;
    }

    @Override
    public List<GpsOrderInfoVO> updateDeviceInfo(String contractNo) {
        List<GpsOrderInfoVO> gpsOrderInfoVOList = new ArrayList<>();
        List<CarGpsDevice> carGpsDeviceList = new ArrayList<>();
        List<CaseCarInfo> caseCarInfoList = carInfoService.list(Wrappers.<CaseCarInfo>lambdaQuery()
                .eq(StringUtils.isNotBlank(contractNo), CaseCarInfo::getContractNo, contractNo));
        for (CaseCarInfo carInfo : caseCarInfoList) {
            // 通过合同编号查询 car_gps_apply 信息
            List<CarGpsApply> gpsApplyList =carGpsApplyService.list(Wrappers.<CarGpsApply>query().lambda().eq(CarGpsApply::getContractNo,contractNo));

            GpsOrderInfoVO gpsOrderInfoVO = new GpsOrderInfoVO();
            JSONObject result = carGpsApplyService.getAllOrderInfoByAppCode(carInfo.getId().toString());
            Assert.isTrue(null != result, "工单号【" + carInfo.getId() + "】查询GPS设备信息异常");

            gpsOrderInfoVO.setStatus(result.getBoolean("Success"));
            if (gpsOrderInfoVO.getStatus()) {
                JSONObject data = result.getJSONObject("Data");
                if (null != data) {
                    JSONObject orderbaseinfo = data.getJSONObject("orderbaseinfo");
                    if (null != orderbaseinfo) {
                        // 这边去更新GPS 传递过来的VIN
                        carGpsApplyService.update(Wrappers.<CarGpsApply>lambdaUpdate().set(CarGpsApply::getFactoryVin,orderbaseinfo.getString("VIN")).eq(CarGpsApply::getId,gpsApplyList.get(0).getId()));

                        gpsOrderInfoVO.setOrderStatus(orderbaseinfo.getString("OrderStatus"));
                        log.info("****************appCode:【{}】工单信息：status：【{}】,desc:【{}",carInfo.getId(),gpsOrderInfoVO.getStatus(),gpsOrderInfoVO.getOrderStatus());
                        gpsOrderInfoVOList.add(gpsOrderInfoVO);
                        //更新GPS派单状态
                        this.updateGpsApplyStatus(contractNo,gpsOrderInfoVO,null);

                        //解析设备信息
                        JSONArray postions = data.getJSONObject("ordereqpositions").getJSONArray("postions");

                        // 声明一个GPS状态 :默认是离线状态
                        String gpsStatus ="1";
                        if (null != postions) {
                            List<Postions> postionsList = JSONObject.parseArray(postions.toJSONString(), Postions.class);
                            for (Postions postionsInfo : postionsList) {
                                CarGpsDevice carGpsDevice = new CarGpsDevice();
                                BeanUtils.copyProperties(postionsInfo, carGpsDevice);
                                carGpsDevice.setCarId(carInfo.getId());
                                carGpsDevice.setApplyNo(carInfo.getApplyNo());
                                carGpsDevice.setContractNo(carInfo.getContractNo());
                                carGpsDeviceList.add(carGpsDevice);
                                gpsStatus=postionsInfo.getGpsRunStatus();
                            }
                        }
                        // 解析安装图片信息
                        JSONArray ordereimgs = data.getJSONArray("ordereimgs");
                        List<GpsOLEEimgs> gpsOLEEimgs = JSONObject.parseArray(ordereimgs.toJSONString(), GpsOLEEimgs.class);
                        for(GpsOLEEimgs gps: gpsOLEEimgs){
                            comAttachmentFileService.remove(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo,contractNo).eq(ComAttachmentFile::getBelongNo,carInfo.getId().toString()).eq(ComAttachmentFile::getAttachmentName,gps.getRemark()));
                            // 去保存GPS图片数据
                            caseTortoiseService.downloadPictureByUrl(contractNo,carInfo.getId().toString(),gps.getHttpurl(),gps.getRemark());
                        }
                        // 去对比 四个维度的数据
                        String vin=orderbaseinfo.getString("VIN");
                        log.info("*****GPS在线状态："+gpsStatus+"****************VIN"+vin+"***********状态："+orderbaseinfo.getString("OrderStatus"));
                        if(gpsApplyList.size()>0){
                            carGpsApplyService.update(Wrappers.<CarGpsApply>lambdaUpdate().set(CarGpsApply::getProof,"").eq(CarGpsApply::getId,gpsApplyList.get(0).getId()));
                            log.info("***********我已经是安装完成******************************************************************");
                            if((!gpsStatus.equals("1"))&&vin.equals(carInfo.getCarVin())&&vin.equals(gpsApplyList.get(0).getCarVin())&&orderbaseinfo.getString("OrderStatus").equals("加装完成")&&gpsOLEEimgs.size()>0){
                                //更新GPS审核状态
                                this.updateStatus(contractNo, AfsEnumUtil.key(GpsStatusEnum.PASS_AUDIT) ,null);

                                //推送数据到进件系统
                                approveLoanInfoService.sendToApplyNotic(contractNo, AfsEnumUtil.key(GpsStatusEnum.PASS_AUDIT));
                            }else {
                                gpsApplyList.get(0).setProof("");
                                if(gpsStatus.equals("1")){
                                    gpsApplyList.get(0).setProof(gpsApplyList.get(0).getProof()+"设备处于离线状态！");
                                }
                                if(orderbaseinfo.getString("OrderStatus").equals("加装完成")){

                                }else {
                                    gpsApplyList.get(0).setProof(gpsApplyList.get(0).getProof()+"GPS状态："+orderbaseinfo.getString("OrderStatus")+"!");
                                }
                                if(vin.equals(carInfo.getCarVin())&&vin.equals(gpsApplyList.get(0).getCarVin())){

                                }else {
                                    gpsApplyList.get(0).setProof(gpsApplyList.get(0).getProof()+"VIN不正确！");
                                }
                                if(gpsOLEEimgs.size()>0){

                                }else {
                                    gpsApplyList.get(0).setProof(gpsApplyList.get(0).getProof()+"无加装信息图片！");
                                }
                                carGpsApplyService.update(Wrappers.<CarGpsApply>lambdaUpdate().set(CarGpsApply::getProof,gpsApplyList.get(0).getProof()).eq(CarGpsApply::getId,gpsApplyList.get(0).getId()));
                            }
                        }

                    }
                }
            }
            //删除历史查询的工单设备信息
            this.delGpsDeviceHistroy(carInfo.getId());
        }
        if(null != carGpsDeviceList){
            //批量保存GPS设备信息
            carGpsDeviceService.saveBatch(carGpsDeviceList);
        }
        return gpsOrderInfoVOList;
    }

    /**
     * @Description 根据GPS接口返回更新GPS派单状态
     * @Author yk.Li
     * @Date 2020/08/11
     */
    private void updateGpsApplyStatus(String contractNo , GpsOrderInfoVO gpsOrderInfoVO ,CaseCarInfo carInfo) {
        if (null != gpsOrderInfoVO && null != gpsOrderInfoVO.getOrderStatus()) {
            switch (gpsOrderInfoVO.getOrderStatus()) {
                case "等待加装":
                    this.updateStatus(contractNo, AfsEnumUtil.key(GpsStatusEnum.WAIT_INSTALL), null != carInfo ? carInfo.getId() : null);
                    break;
                case "加装完成":
                    this.updateStatus(contractNo, AfsEnumUtil.key(GpsStatusEnum.COMPLETE_INSTALL), null != carInfo ? carInfo.getId() : null);
                    break;
                default:
//                    this.updateStatus(contractNo, AfsEnumUtil.key(GpsStatusEnum.UN_SEND_TASK), null != carInfo ? carInfo.getId() : null);
                    break;
            }
        }
    }

    /**
     * @Description 删除历史查询的工单设备信息
     * @Author yk.Li
     * @Date 2020/08/08
     */
    public void delGpsDeviceHistroy(Long carId) {
        List<CarGpsDevice> carGpsDeviceList = carGpsDeviceService.list(Wrappers.<CarGpsDevice>lambdaQuery()
                .eq(CarGpsDevice::getCarId, carId));
        for (CarGpsDevice carGpsDevice : carGpsDeviceList) {
            carGpsDeviceService.removeById(carGpsDevice.getId());
        }
    }
}
